<?php
namespace app\admin\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        $data = model('Releases')->dataByAllStatus();

        if(empty($data[0]['id'])) {
            $this->error('id不合法');
        }
        return $this->fetch('',[
            'data' => $data,
        ]);
    }

    public function status(){
        $data = input('get.');
        $validate = validate('Releases');
        if(!$validate->scene('status')->check($data)) {
            $this ->error($validate ->getError());
        }
        $res = model('Releases')->save(['status' =>$data['status']],
            ['id'=>$data['id']]);
        if($res){
            $this->success('状态跟新成功');
        }else{
            $this->error('状态跟新失败');
        }
    }

    public function edit($id=0){
        if(intval($id) < 1){
            $this->error('参数不合法');
        }
        $data= model('Releases')->get($id);

        return $this ->fetch('', [
            'data'=>$data,
        ]);
    }

    public function save(){

        if(!request()->isPost()){
            $this->error('请求失败');
        }
        $data = input('post.');
        $id = input('get.id');

        $validate = validate('Releases');
        if(!$validate->scene('add')->check($data)) {
            $this ->error($validate ->getError());
        }
        if(!empty($id)){
            return $this->update($data,$id);
        }
        //把$data数据提交给model层
        $res = model('Releases')->add($data);
        if($res){
            $this->success('新增成功',url('index/index'));
        }else{
            $this->error('新增失败');
        }
    }


    public function update($data,$id){
        $res = model('Releases')->save($data,['id'=>intval($id)]);
        if($res){
            $this->success('更新成功',url('index/index'));
        }else{
            $this->error('更新失败');
        }
    }



}
